<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery.sourceState" class="el-menu-demo" mode="horizontal" @select="handleSelect">
        <el-menu-item index="" >全部</el-menu-item>
        <el-menu-item index="0">上架中</el-menu-item>
        <el-menu-item index="30">已成交</el-menu-item>
        <el-menu-item index="20">手动下架</el-menu-item>
       <el-menu-item index="10">自动下架</el-menu-item>
    </el-menu>

    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="销售订单号：">
          <el-input v-model="listQuery.orderSn"></el-input>
        </el-form-item>

        <el-form-item label="装货地址：">
          <el-input v-model="listQuery.sendAddress"></el-input>
<!--          <el-cascader-->
<!--            v-model="listQuery.sendAddress"-->
<!--            :props="{ value: 'areaName',label:'areaName',children:'areaChildList' }"-->
<!--            :options="options"></el-cascader>-->
        </el-form-item>
        <el-form-item label="卸货地址：">
          <el-input v-model="listQuery.receiveAddress"></el-input>
<!--          <el-cascader-->
<!--            v-model="listQuery.receiveAddress"-->
<!--            :props="{ value: 'areaName',label:'areaName',children:'areaChildList' }"-->
<!--            :options="options"></el-cascader>-->
        </el-form-item>
        <el-form-item label="发布日期：">
          <el-date-picker
                  v-model="listQuery.createTime"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货人：" >
          <el-select v-model="listQuery.createUserId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container">
      <el-table
              v-loading="listLoading"
              :data="tableData"
              border
              style="width: 100%; margin-top: 20px">
        <el-table-column  label="销售订单" min-width="180">
          <template slot-scope="scope">
            <el-link @click="toOrderDetail(scope.row)" type="primary">
              <span style="color:#606266">销售订单:</span>{{ scope.row.orderSn }}
            </el-link>
            <el-link @click="toDeliverCon(scope.row)" type="primary" v-if="scope.row.deliverId != ''">
              <span style="color:#606266">发货单：</span>{{ scope.row.deliverSn }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column  label="路线" min-width="300" >
          <template slot-scope="scope">
              <div>发货地址：{{ scope.row.sendAddress }}</div>
              <div>收货地址：{{ scope.row.receiveAddress }}</div>
          </template>
        </el-table-column>
        <el-table-column  label="车型/车长" min-width="150" align="center">
            <template slot-scope="scope">
                <span style="vertical-align: middle">{{scope.row.truckType}},{{scope.row.truckLength}}</span>
            </template>
        </el-table-column>
        <el-table-column  label="货物信息" min-width="150" align="center">
            <template slot-scope="scope">
                <div>{{ scope.row.goodsName }}/{{ scope.row.goodsNum }}吨</div>
            </template>
        </el-table-column>
        <el-table-column  label="出价模式" min-width="100" align="center">
            <template slot-scope="scope">
              {{ scope.row.dealMode }}
            </template>
        </el-table-column>
        <el-table-column  label="费用信息" min-width="150" align="center">
          <template slot-scope="scope">
              <div>总费用：￥{{ scope.row.freightAmount }}</div>
              <div>订金：￥{{scope.row.deposit}}</div>
          </template>
        </el-table-column>

        <el-table-column  label="发票信息" min-width="100" align="center">
          <template slot-scope="scope">
                {{scope.row.isNeedInvoice == 1 ? '开发票' : '不开发票'}}
          </template>
        </el-table-column>

        <el-table-column  label="货源状态" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.sourceState  | fifstate}}
          </template>
        </el-table-column>

        <el-table-column  label="发货人" min-width="150" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.createUserName }} <span style="padding-left:10px">{{scope.row.createUserMobile}}</span></div>
          </template>
        </el-table-column>

        <el-table-column  label="发布时间" min-width="150" align="center">
          <template slot-scope="scope">
              {{scope.row.createTime}}
          </template>
        </el-table-column>

        <el-table-column
                width="100"
                fixed="right"
                label="操作"
                align="center">
          <template slot-scope="scope">
                <el-button type="text" size="mini" @click="showPop(scope.row)">查看</el-button>
                <el-button type="text" size="mini" @click="handleConfirm(scope.row)" v-if="scope.row.sourceState == 0">下架</el-button>
                <el-button type="text" size="mini" @click="toDetail(scope.row)" v-if="scope.row.sourceState == 30">查看订单</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="total, sizes,prev, pager, next,jumper"
              :current-page.sync="listQuery.pageNum"
              :page-size="listQuery.pageSize"
              :page-sizes="[10,15,20]"
              :total="count">
      </el-pagination>
    </div>


      <!--查看货源-->
      <sourceGoods ref="sourceGoods" @handleConfirm="handleConfirm"></sourceGoods>


  </div>
</template>
<script>
  import {getCustomerOption,getOrderUsersList,getDeliverList,deliverUpdate,getQiniuToken,postDeliverVoucher,exportDeliverList,getAddressList} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import sourceGoods from '@/views/components/sourceGoods'
  import {validateCommission} from '@/utils/validate';

  import store from '@/store'
  import { mapGetters } from 'vuex'
  import {delSource, sourceInfo, sourceList} from "../../api/partner";
  const defaultListQuery={
    "orderSn":"",  //销售订单号
    "sendAddress":"", //发货地址
    "receiveAddress":"", //收货地址
    "createTime":[], //发布时间
    "createUserId":'',  //发货人
    "sourceState":"", //货源状态(0上架中，10自动下架，20手动下架 30已成交)
    "pageNum":1,
    "pageSize":10
  };

  export default {
    name: "sourceList",
    data(){
        const isvalidateCommission = (rule, value, callback) => {
            if (!validateCommission(parseFloat(value))) {
                callback(new Error('非负整数或小数'))
            } else if(value > 99999999){
                callback(new Error('最大值为99999999'))
            }else {
                callback()
            }
        };
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        userOptionList:[],
        options:[],

      }
    },
    components : {
      sourceGoods:sourceGoods
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),


    },
    created() {
       this.getAddressListData()
       this.getList();
       this.getUserOptionList();

    },
    filters: {
      formatCreateTime(time) {
        if(time){
          let date = new Date(time);
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        }else{
          return '--'
        }
      },
      fifstate(val){
          if(val == 0){
            return '上架中'
          }else if(val == 10){
            return '自动下架'
          }else if(val == 20){
            return '手动下架'
          }else if(val == 30){
            return '已成交'
          }
      },
    },
    methods:{
      toDetail(item){
        let routeData = this.$router.resolve({name:'ymmOrderInfo',query:{ymmOrderId:item.ymmOrderId}});
        window.open(routeData.href);
      },
      handleConfirm(item){
        this.$confirm('确认下架此货源吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delSource({ymmCargoId:item.ymmCargoId}).then(()=>{
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.handleSearchList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      },
      //获取省市区列表
      getAddressListData(){
        getAddressList().then((res)=>{
          this.options = res;
        })
      },

        // showMap(){
        //   this.$refs.trajectoryMap.showMap();
        // },

         //显示货源详情
        showPop(item){
         this.$refs.sourceGoods.showPop(item,'1')
        },

        //查看发货单
        toDeliverCon(item){
            let routeData = this.$router.resolve({name:'deliverCon',query:{id:item.deliverId,type:'edit'}});
            window.open(routeData.href);
        },

        //查看订单
        toOrderDetail(item){
          let routeData = this.$router.resolve({name:'deliverOrderInfo',query:{id:item.orderId}});
          window.open(routeData.href);
        },



      //获取员工下拉选项
      getUserOptionList(){
        getOrderUsersList().then((res)=>{
          this.userOptionList = res;
        })
      },


      /**
       * 切换选择状态
       */
      handleSelect(key, keyPath){
        this.listQuery = JSON.parse(JSON.stringify(defaultListQuery)),
        this.listQuery.sourceState = key;
        this.handleSearchList();
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取货源列表
      getList() {
        this.listLoading = true;
        sourceList(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },



    },
  }
</script>

<style lang="scss">
   .adr-info{
      border-radius: 5px;
      border:1px solid #67c23a;
      overflow: hidden;
      background-color: #f0f9eb;
      .title{
        color: #67c23a;
        font-size:14px;
        font-weight: bold;
        text-align: center;
        line-height: 30px;
      }
     .con{
       text-align: left;
       padding:0 10px 10px 10px;
       .big-font{
         font-size:14px;
         color:#333;
         font-weight: bold;
         margin-bottom:10px;
       }
       .small-font{
         font-size:12px;
         color:#333;
         line-height: 20px;
       }
     }
   }
   .adr-info.yellow{
     border:1px solid #e6a23c;
     background-color: #fdf6ec;
     .title{
       color: #e6a23c;
       font-size:14px;
       font-weight: bold;
       text-align: center;
       line-height: 30px;
     }
   }


</style>
